{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"https://raw.githubusercontent.com/Qiskit/qiskit-tutorials/master/images/qiskit-heading.png\" alt=\"Note: In order for images to show up in this jupyter notebook you need to select File => Trusted Notebook\" width=\"500 px\" align=\"left\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Quantum Slot Machine\n",
    "\n",
    "### Here we will use the intrinsic randomness of quantum mechanics to control the selection of icons on a slot machine."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-12-28T15:54:29.168824Z",
     "start_time": "2018-12-28T15:54:29.093354Z"
    },
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a800b89078844bb399a15d512fda176a",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "VBox(children=(VBox(children=(Image(value=b'\\x89PNG\\r\\n\\x1a\\n\\x00\\x00\\x00\\rIHDR\\x00\\x00\\x03\\xc9\\x00\\x00\\x00\\xc…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from game_engines.quantum_slot import quantum_slot_machine\n",
    "quantum_slot_machine()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Paytable"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"./game_engines/quantum_slot/machine/paytable.png\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## How to play\n",
    "\n",
    "To play, select a backend to use to generate the random numbers in the lower left of the machine.  By default this is the `qasm_simulator`. When the machine is started, a job is sent to the `ibmq_5_tenerife` device requesting some random bit strings.  The progess of this job can be tracked in the lower right.  When done, you can select the `ibmq_5_tenerife` backend to use these numbers.  Push the slot machine handle button to run the machine.  You initially get 20 credits, and play continues until you stop or your credits run out.  Payout is according to the table above."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## How it works\n",
    "\n",
    "There are eight choices of symbols that can be displayed for the three slots on the machine.  To uniquely select between them, we need $3=\\log_{2}(8)$ bits on information.  The quantum slot machine uses three different backends to generate the random bit strings of length three that select which symbol to show on the wheel:\n",
    "\n",
    "* **qasm_simulator**\n",
    "Uses the local qasm simulator in Qiskit terra to pseudo-randomly generate bit strings from a single-shot of quantum circuit that consists of Hadamard gates on nine different qubits followed by measurements.\n",
    "\n",
    "\n",
    "* **Computation**\n",
    "Computes $300$ shots of a three-qubit variant of the same circuit used in the simulator. After running once, this selection will allow you to use the machine $100$ times.\n",
    "\n",
    "\n",
    "* **ANU QRNG**\n",
    "The random numbers generated from the circuits used above suffer from the fact that unavoidable energy relaxation makes the lower bit-strings slightly more favorable than others.  As such, it would be preferable to find a platform that generates random numbers from the quantum ground state of a system.  Here we use the [Australia National University Quantum Random Number Generator](https://qrng.anu.edu.au/) that measures the random fluctuations of the vacuum state of a quantum oscillator."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "keywords = {'Topics': ['Games','Random number generation'], 'Commands': ['`h`','`shots=1`']}"
   ]
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
